C语言常用的math函数

您所在的位置:网站首页 c 有哪些库函数 C语言常用的math函数

C语言常用的math函数

2023-10-11 21:53| 来源: 网络整理| 查看: 265

注意:使用时需要在在程序开头加上math.h头文件。

1、fabs(double x)

    对double型的变量取绝对值.示例:

#include #include int main() { double a=-13.14; printf("%.2f",fabs(a)); return 0; }

输出:13.14

2、floor(double x) 和ceil(double y)

向下取整和向上取整,返回类型为double.示例:

#include #include int main() { double a=-5.2,b=5.2; printf("%.0f %.0f\n",floor(a),ceil(a)); printf("%.0f %.0f\n",floor(b),ceil(b)); return 0; }

输出:-6 -5

            5 6

3、pow(double x,double y)和sqrt(double x)

   平方函数和平方根函数。示例:

#include #include int main() { double a=pow(2.0,3.0); double b=sqrt(2.0); printf("%f %f",a,b); return 0; }

输出:8.00000  1.414214

4、sin(double x)、cos(double y)和tan(double x)

  三角函数,反三角函数用法一样,在名字前加a如:asin(double x)。示例:

#include #include const double pi=acos(-1.0); int main() { double a=sin(pi*45/180); double b=cos(pi*45/180); double c=tan(pi*45/180); printf("%f %f %f",a,b,c); return 0; }

输出:0.707107   0.707107   1.000000

5、round(double x)

  对double 型变量进行四舍五入,返回值也是double类型。示例:

#include #include int main() { double a=round(5.20),b=round(5.80); printf("%d %d",a,b); return 0; }

输出:5 6



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3